package com.moon.game.resources;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.moon.game.cache.Memcached;
import com.moon.game.constants.GlobalConstants;
import com.moon.game.exception.GException;
import com.moon.game.resource.model.Animal;
import com.moon.game.resource.model.AnimalChild;
import com.moon.game.resource.model.Gift;
import com.moon.game.resource.model.Item;
import com.moon.game.resource.model.MainLand;
import com.moon.game.resource.model.Mission;
import com.moon.game.util.Utils;

/**
 * 
 * 加载数据库资源
 * 
 * @author Ranger
 *
 */
public class DBResource {
	private static DBResource instance = new DBResource();
	private static Map<Integer, Animal> animalRes = new HashMap<Integer, Animal>();
	private static Map<Integer, AnimalChild> animalChildRes = new HashMap<Integer, AnimalChild>(); 
	private static Map<Integer, Item> itemRes = new HashMap<Integer, Item>();
	private static Map<Integer, Mission> missionRes = new HashMap<Integer, Mission>();
	private static Map<Integer, MainLand> mainlandRes = new HashMap<Integer, MainLand>();
	private static Map<Integer, ArrayList<Integer>> portsRes = new HashMap<Integer, ArrayList<Integer>>();
	private static Map<Integer, Gift> giftRes = new HashMap<Integer, Gift>();
	private DBResource() {}
	
	@SuppressWarnings("unchecked")
	public static void initResource() throws GException{
		if (instance == null) {
			instance = new DBResource();
		}
		animalRes = (Map<Integer, Animal>) Memcached.get(GlobalConstants.POOL_NAME_SYS, "animal_resource");
		if (animalRes == null) {
			throw new GException("animalRes is null");
		}
		
		animalChildRes = (Map<Integer, AnimalChild>) Memcached.get(GlobalConstants.POOL_NAME_SYS, "animal_child_resource");
		if (animalChildRes == null) {
			throw new GException("animalChildRes is null");
		}
		
		itemRes = (Map<Integer, Item>) Memcached.get(GlobalConstants.POOL_NAME_SYS, "item_resource");
		if (itemRes == null) {
			throw new GException("itemRes is null");
		}
		
		missionRes = (Map<Integer, Mission>) Memcached.get(GlobalConstants.POOL_NAME_SYS, "mission_resource");
		if (missionRes == null) {
			throw new GException("missionRes is null");
		}
		
		mainlandRes = (Map<Integer, MainLand>) Memcached.get(GlobalConstants.POOL_NAME_SYS, "mainland_resource");
		if (missionRes == null) {
			throw new GException("mainlandRes is null");
		}
		
		portsRes = (Map<Integer, ArrayList<Integer>>) Memcached.get(GlobalConstants.POOL_NAME_SYS, "ports_resource");
		if (portsRes == null) {
			throw new GException("portsRes is null");
		}
		
		giftRes = (Map<Integer, Gift>) Memcached.get(GlobalConstants.POOL_NAME_SYS, "gift_resource");
		if (giftRes == null) {
			throw new GException("giftRes is null");
		}
	}
	/**根据animalId获得珍兽*/
	public static final Animal getAnimalRes(int animalId) {
		return animalRes.get(animalId);
	}
	/**根据珍兽孵化资源*/
	public static final AnimalChild getAnimalChildRes(int animalId) {
		return animalChildRes.get(animalId);
	}
	/**根据itemId获得道具*/
	public static final Item getItemRes(int itemId) {
		return itemRes.get(itemId);
	}
	/**根据missionId获得道具*/
	public static final Mission getMissionRes(int missionId) {
		return missionRes.get(missionId);
	}
	
	/**任务道具奖励列表解析*/
	public static HashMap<Integer, Integer> getMissionItemsReward(int missionId) {
		Mission mission = getMissionRes(missionId);
		String itemRes = mission.getRewardItemsNums();
		HashMap<Integer, Integer> itemNum = new HashMap<Integer, Integer>();
		if (itemRes == null || itemRes.length() == 0) {
			return itemNum;
		}
		itemNum.putAll(Utils.analyzeString(itemRes));
		return itemNum;
	}
	
	/**任务道具消耗列表解析*/
	public static HashMap<Integer, Integer> getMissionItemsUsed(int missionId) {
		Mission mission = getMissionRes(missionId);
		String itemUsed = mission.getItemNum();
		HashMap<Integer, Integer> itemNum = new HashMap<Integer, Integer>();
		if (itemUsed == null || itemUsed.length() == 0) {
			return itemNum;
		}
		itemNum.putAll(Utils.analyzeString(itemUsed));
		return itemNum;
	}
	
	
	/**根据landId获得大陆信息*/
	public static final MainLand getMainlandRes(int landId) {
		return mainlandRes.get(landId);
	}
	
	/**根据landId获得大陆传送门信息*/
	public static final ArrayList<Integer> getPortsRes(int landId) {
		return portsRes.get(landId);
	}
	
	/**礼包资源*/
	public static Gift getGiftRes(int giftId) {
		return giftRes.get(giftId);
	}
	
	/**
	 * 打开礼包获得道具列表
	 * 
	 * @param giftId
	 * @param isGift true获取的礼包列表，false获取的道具列表
	 * @return
	 */
	public static HashMap<Integer, Integer> getRewardItems(int giftId, boolean isGift) {
		Gift gift = getGiftRes(giftId);
		String itemReward = gift.getItemNum();
		if (itemReward == null || itemReward.length() == 0) {
			return null;
		}
		HashMap<Integer, Integer> allReardItems = Utils.analyzeString(itemReward);
		HashMap<Integer, Integer> items = new HashMap<Integer, Integer>();
		if (allReardItems != null && allReardItems.size() > 0) {
			for (Map.Entry<Integer, Integer> entry : allReardItems.entrySet()) {
				if (isGift) {
					if (getItemRes(entry.getKey()) != null) {
						continue;
					}
					items.put(entry.getKey(), entry.getValue());
				}else {
					if (getItemRes(entry.getKey()) == null) {
						continue;
					}
					items.put(entry.getKey(), entry.getValue());
				}
			}
		}
		return items;
	}

}
